package containers;

import java.util.*;
import net.mindview.util.*;

public class SlowMap<K,V> extends AbstractMap<K,V> {
    private List<K> keys=new ArrayList<K>();
    private List<V> values=new ArrayList<V>();
    private EntrySet entries=new EntrySet();
    @Override
    public V put(K key, V value) {
        V oldValue=get(key);
        if(!keys.contains(key)){
            keys.add(key);
            values.add(value);
        }else
            values.set(keys.indexOf(key),value);
        return oldValue;
    }

    public V get(Object key){
        if(!keys.contains(key))
            return null;
        return values.get(keys.indexOf(key));
    }

    @Override
    public Set<Map.Entry<K, V>> entrySet() {
        return entries;
    }

    private class EntrySet extends AbstractSet<Map.Entry<K,V>>{
       @Override
       public int size() {
           return keys.size();
       }

       @Override
       public Iterator<Map.Entry<K, V>> iterator() {
           return new Iterator<Map.Entry<K, V>>() {
               private int index=0;
               @Override
               public boolean hasNext() {
                   return index<keys.size()-1;
               }

               @Override
               @SuppressWarnings("unchecked")
               public Map.Entry<K, V> next() {
                   index++;
                   return new MapEntry<K, V>(keys.get(index),values.get(index));
               }

               @Override
               public void remove() {
                    keys.remove(index--);
                    values.remove(index--);
               }
           };
       }
   }

    public static void main(String[] args) {
        SlowMap<String,String> m=new SlowMap<String,String>();
        m.putAll(Countries.capitals(15));
        System.out.println(m);
        System.out.println(m.get("BULGARIA"));
        System.out.println(m.entrySet());
    }
}
